/* BDVB-App
Copyright (C) 2014 Sascha Manns <Sascha.Manns@outlook.de>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/


using System;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Net.NetworkInformation;

using Windows.UI.Xaml;

using AppStudio.Services;
using AppStudio.Data;

namespace AppStudio.ViewModels
{
    public class MainViewModel : BindableBase
    {
       private DerBDVBViewModel _derBDVBModel;
       private GliederungenViewModel _gliederungenModel;
       private FacebookViewModel _facebookModel;
       private KontaktViewModel _kontaktModel;
       private AboutViewModel _aboutModel;
        private PrivacyViewModel _privacyModel;

        private ViewModelBase _selectedItem = null;

        public MainViewModel()
        {
            _selectedItem = DerBDVBModel;
            _privacyModel = new PrivacyViewModel();

        }
 
        public DerBDVBViewModel DerBDVBModel
        {
            get { return _derBDVBModel ?? (_derBDVBModel = new DerBDVBViewModel()); }
        }
 
        public GliederungenViewModel GliederungenModel
        {
            get { return _gliederungenModel ?? (_gliederungenModel = new GliederungenViewModel()); }
        }
 
        public FacebookViewModel FacebookModel
        {
            get { return _facebookModel ?? (_facebookModel = new FacebookViewModel()); }
        }
 
        public KontaktViewModel KontaktModel
        {
            get { return _kontaktModel ?? (_kontaktModel = new KontaktViewModel()); }
        }
 
        public AboutViewModel AboutModel
        {
            get { return _aboutModel ?? (_aboutModel = new AboutViewModel()); }
        }

        public void SetViewType(ViewTypes viewType)
        {
            DerBDVBModel.ViewType = viewType;
            GliederungenModel.ViewType = viewType;
            FacebookModel.ViewType = viewType;
            KontaktModel.ViewType = viewType;
            AboutModel.ViewType = viewType;
        }

        public ViewModelBase SelectedItem
        {
            get { return _selectedItem; }
            set
            {
                SetProperty(ref _selectedItem, value);
                UpdateAppBar();
            }
        }

        public Visibility AppBarVisibility
        {
            get
            {
                return SelectedItem == null ? AboutVisibility : SelectedItem.AppBarVisibility;
            }
        }

        public Visibility AboutVisibility
        {

         get { return Visibility.Visible; }
        }

        public void UpdateAppBar()
        {
            OnPropertyChanged("AppBarVisibility");
            OnPropertyChanged("AboutVisibility");
        }

        /// <summary>
        /// Load ViewModel items asynchronous
        /// </summary>
        public async Task LoadData(bool isNetworkAvailable)
        {
            var loadTasks = new Task[]
            { 
                DerBDVBModel.LoadItems(isNetworkAvailable),
                GliederungenModel.LoadItems(isNetworkAvailable),
                FacebookModel.LoadItems(isNetworkAvailable),
                KontaktModel.LoadItems(isNetworkAvailable),
                AboutModel.LoadItems(isNetworkAvailable),
            };
            await Task.WhenAll(loadTasks);
        }

        /// <summary>
        /// Refresh ViewModel items asynchronous
        /// </summary>
        public async Task RefreshData(bool isNetworkAvailable)
        {
            var refreshTasks = new Task[]
            { 
                DerBDVBModel.RefreshItems(isNetworkAvailable),
                GliederungenModel.RefreshItems(isNetworkAvailable),
                FacebookModel.RefreshItems(isNetworkAvailable),
                KontaktModel.RefreshItems(isNetworkAvailable),
                AboutModel.RefreshItems(isNetworkAvailable),
            };
            await Task.WhenAll(refreshTasks);
        }

        //
        //  ViewModel command implementation
        //
        public ICommand RefreshCommand
        {
            get
            {
                return new DelegateCommand(async () =>
                {
                    await RefreshData(NetworkInterface.GetIsNetworkAvailable());
                });
            }
        }

        public ICommand AboutCommand
        {
            get
            {
                return new DelegateCommand(() =>
                {
                    NavigationServices.NavigateToPage("AboutThisAppPage");
                });
            }
        }

        public ICommand PrivacyCommand
        {
            get
            {
                return new DelegateCommand(() =>
                {
                    NavigationServices.NavigateTo(_privacyModel.Url);
                });
            }
        }
    }
}
